﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CookComputing.XmlRpc;
using System.Reflection;
using System.Collections;
using Microsoft.Phone.Controls;
using System.IO;

namespace HomeWP
{
    public partial class SHUTTERCONTACT : UserControl
    {
        public SHUTTERCONTACT()
        {
            InitializeComponent();
        }

        public Homematic proxy;
        public string Address { get; set; }
        public string DisplayName { get; set; }
        public string CCUIP { get; set; }
        public int CCUPort { get; set; }
        public Device HMDevice { get; set; }
        private bool isLoaded = true;

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            toggleSwitch1.Header = DisplayName;
            toggleSwitch1.IsEnabled = false;
            proxy = new Homematic();
            if (!string.IsNullOrEmpty(CCUIP))
            {
                proxy.Url = "http://" + CCUIP + ":" + CCUPort;
            }
            proxy.GetValue(Address, this);

            toggleSwitch1.Checked += new EventHandler<RoutedEventArgs>(toggleSwitch1_Checked);
            toggleSwitch1.Unchecked += new EventHandler<RoutedEventArgs>(toggleSwitch1_Unchecked);
            
            toggleSwitch1.IsEnabled = true;


        }

        void toggleSwitch1_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isLoaded & toggleSwitch1.IsEnabled)
            {
                //proxy.SetValue(Address, false);
                //toggleSwitch1.Content = "geschlossen";
            }

        }

        void toggleSwitch1_Checked(object sender, RoutedEventArgs e)
        {
            if (isLoaded & toggleSwitch1.IsEnabled)
            {
                //proxy.SetValue(Address, true);
                //toggleSwitch1.Content = "offen";
            }
        }



        [XmlRpcUrl(Homematic.UrlCCU)]
        public class Homematic : XmlRpcClientProtocol
        {
            //Default URL
            public const string UrlCCU = "http://192.168.2.15:2001";

            [XmlRpcEnd]
            public object EndGetValue(IAsyncResult iasr)
            {
                return this.EndInvoke(iasr);
            }

            [XmlRpcBegin("getValue")]
            internal IAsyncResult getValue(string address, string valueKey, AsyncCallback acb)
            {
                return this.BeginInvoke(MethodBase.GetCurrentMethod(), new object[] { address, valueKey }, acb, null);
            }

            [XmlRpcBegin("setValue")]
            internal IAsyncResult setValue(String address, String valueKey, ValueType value, AsyncCallback acb)
            {
                return this.BeginInvoke(MethodBase.GetCurrentMethod(), new object[] { address, valueKey, value }, acb, null);
            }

            public void GetValue(string Address, SHUTTERCONTACT oBox)
            {
                getValue(Address, "STATE", asr =>
                {
                    ((App)App.Current).RootFrame.Dispatcher.BeginInvoke(delegate()
                    {
                        try
                        {
                            var posts = EndGetValue(asr);
                            string sResult = posts.ToString();
                            try
                            {
                                //oBox.toggleSwitch1.Header = Address;
                                oBox.toggleSwitch1.IsChecked = bool.Parse(sResult);
                                if (oBox.toggleSwitch1.IsChecked.Value)
                                    oBox.toggleSwitch1.Content = "offen";
                                else
                                    oBox.toggleSwitch1.Content = "geschlossen";
                            }
                            catch { }

                        }
                        catch (Exception ex)
                        {
                            // Handle exception here
                        }
                    });

                });
            }


            public void SetValue(string Address, ValueType Value)
            {
                setValue(Address, "STATE", Value, asr =>
                {
                    ((App)App.Current).RootFrame.Dispatcher.BeginInvoke(delegate()
                    {
                        try
                        {
                            var posts = EndGetValue(asr);
                        }
                        catch (Exception ex)
                        {
                            // Handle exception here
                        }
                    });

                });
            }
        }

    }


}
